package com.onetech.adrewards.free.activities;

import java.util.List;

import jp.co.CAReward_Ack.CARController;
import jp.co.CAReward_Common.CARCheck;
import jp.co.CAReward_Media.CARMCommon;
import jp.co.CAReward_Media.CARMIntent;
import android.annotation.TargetApi;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.onetech.adrewards.free.R;


public class TestCA extends BaseActivity{

	public EditText edtPlatformId;
	public EditText edtOwnerId;
	public EditText edtMid;
	public EditText edtUserId;
	public EditText edtUrl;
	public EditText edtApiKey;
	public EditText edtAppKey;
	public EditText edtLoadingMsg;
	public EditText edtTimeout;
	public Button btnContent;
	public TextView textLog;
	
	@TargetApi(Build.VERSION_CODES.GINGERBREAD_MR1)
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        setMainView(R.layout.activity_main);

        CARController.appkey = "ack_appkey";
        CARController.cid = "cid";
        CARController.pid = "1";
        CARController.mcwait = false;
        CARController.nor = 1;
        CARController.cpi = "1";
        CARController.analytics = false;
        CARController.notifyAppLaunch(this.getApplicationContext(), this.getIntent());        
        
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD_MR1) {
        	StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitAll().build());
        }
        
        edtPlatformId = (EditText)findViewById(R.id.edt_platform_id);
        edtOwnerId = (EditText)findViewById(R.id.edt_m_owner_id);
        edtMid = (EditText)findViewById(R.id.edt_m_id);
        edtUserId = (EditText)findViewById(R.id.edt_user_id);
        edtUrl = (EditText)findViewById(R.id.edt_url);
        edtApiKey = (EditText)findViewById(R.id.edt_api_key);
        edtAppKey = (EditText)findViewById(R.id.edt_app_key);
        edtLoadingMsg = (EditText)findViewById(R.id.edt_loading_msg);
        edtTimeout = (EditText)findViewById(R.id.edt_timeout);
        btnContent = (Button)findViewById(R.id.btn_content);
        textLog = (TextView)findViewById(R.id.txt_log);
        
        edtPlatformId.setText("0");

        edtOwnerId.setText("558");
        edtMid.setText("2835");
        edtUserId.setText("CAR20130118001");
        edtAppKey.setText("ncIdX3la");
        edtApiKey.setText("76552d4faa4dd35b");
        edtUrl.setText("http://car.mobadme.jp/spg/spnew/558/2835/index.php");

        
        
        
        
        
//        edtOwnerId.setText("51");
//        edtMid.setText("282");
//        edtUserId.setText("CAR");
//        edtApiKey.setText("9914d495e84ee3f6");
//        edtUrl.setText("http://ajmp.jp/apl/android/ameba/index.php");
        //edtUrl.setText("http://car.mobadme.jp:8001/spg/sp/ad/_test/beeline_smurf/index.php");
        edtUrl.setText("http://car.mobadme.jp/spg/spnew/558/2835/index.php");
        
/**        
        edtMid.setText("345");
        edtUserId.setText("CAR20130118001");
        edtApiKey.setText("8494d9c72d73f796");
        edtUrl.setText("http://car.mobadme.jp/apl/wall2013/android/328/index.php");
**/
        
        //edtAppKey.setText("ncIdX3la");
        //edtLoadingMsg.setText("Now Loading...");
        //edtTimeout.setText("30000");
        
//        edtUrl.setText("http://car.mobadme.jp/apl/328/1805/index.php");
//        edtUrl.setText("http://car.mobadme.jp/apl/wall2013/android/index.php");
        //edtAppKey.setText("ncIdX3la");
        edtLoadingMsg.setText("Now Loading...");
        edtTimeout.setText("30000");
        
        String strLog = "";
        CARCheck check = new CARCheck(getApplicationContext());
        if(check.isSuperUserCustomized()) strLog += "RootåŒ–:trueÂ¥n";
        else strLog += "RootåŒ–:falseÂ¥n";

        strLog += "--- ã‚µãƒ¼ãƒ�æ—¥æ™‚ -----Â¥n";
        String http_date = "";
        int int_timeout = Integer.parseInt(edtTimeout.getText().toString());
        CARMCommon mcomm = new CARMCommon();
        http_date = mcomm.getHttpDate(int_timeout);
        strLog += http_date + "Â¥n";

        strLog += "--- ajmpãƒ†ã‚¹ãƒˆæŽ¥ç¶š -----Â¥n";
        String http_result = "";
        http_result = mcomm.getHttpResult("http://ajmp.jp:8001/apl/android/ameba_test/aaa.html", int_timeout);
        strLog += http_result + "Â¥n";

        strLog += "--- new uuid -----Â¥n";
        String[] new_uuid = mcomm.getUuid(edtAppKey.getText().toString(),int_timeout);
		if(new_uuid != null && new_uuid.length > 0) strLog += "new_uuid:" + String.valueOf(new_uuid[0]) + "Â¥n";
		if(new_uuid != null && new_uuid.length > 1) strLog += "new_uuid_dec:" + String.valueOf(new_uuid[1]) + "Â¥n";
		if(new_uuid != null && new_uuid.length > 2) strLog += "new_uuid_uuid:" + String.valueOf(new_uuid[2]) + "Â¥n";
		if(new_uuid != null && new_uuid.length > 3) strLog += "new_uuid_date:" + String.valueOf(new_uuid[3]) + "Â¥n";        
		if(new_uuid == null) strLog += "new_uuid:nullÂ¥n";

        strLog += "--- HTTP uuid -----Â¥n";
        String http_uuid[] = null;
        http_uuid = mcomm.getUuidHttp(edtOwnerId.getText().toString(), edtUserId.getText().toString(), edtPlatformId.getText().toString(), edtAppKey.getText().toString(), int_timeout);
		if(http_uuid != null && http_uuid.length > 0) strLog += "http_uuid:" + String.valueOf(http_uuid[0]) + "Â¥n";
		if(http_uuid != null && http_uuid.length > 1) strLog += "http_uuid_dec:" + String.valueOf(http_uuid[1]) + "Â¥n";
		if(http_uuid != null && http_uuid.length > 2) strLog += "http_uuid_uuid:" + String.valueOf(http_uuid[2]) + "Â¥n";
		if(http_uuid != null && http_uuid.length > 3) strLog += "http_uuid_date:" + String.valueOf(http_uuid[3]) + "Â¥n";
		if(http_uuid == null) strLog += "http_uuid:nullÂ¥n";
        
        strLog += "--- SharedPreferences uuid -----Â¥n";
        String spf_uuid[] = null;
        spf_uuid = mcomm.getUuidSpf(getApplicationContext(), edtAppKey.getText().toString());
		if(spf_uuid != null && spf_uuid.length > 0) strLog += "spf_uuid:" + String.valueOf(spf_uuid[0]) + "Â¥n";
		if(spf_uuid != null && spf_uuid.length > 1) strLog += "spf_uuid_dec:" + String.valueOf(spf_uuid[1]) + "Â¥n";
		if(spf_uuid != null && spf_uuid.length > 2) strLog += "spf_uuid_uuid:" + String.valueOf(spf_uuid[2]) + "Â¥n";
		if(spf_uuid != null && spf_uuid.length > 3) strLog += "spf_uuid_date:" + String.valueOf(spf_uuid[3]) + "Â¥n";        
		if(spf_uuid == null) strLog += "spf_uuid:nullÂ¥n";
        
        strLog += "--- ã‚¤ãƒ³ã‚¹ãƒˆãƒ¼ãƒ«æ¸ˆãƒ‘ãƒƒã‚±ãƒ¼ã‚¸ -----Â¥n";
        PackageManager pm = this.getPackageManager();
        List<ApplicationInfo> alist = pm.getInstalledApplications(PackageManager.GET_META_DATA);
        ApplicationInfo[] apps = alist.toArray(new ApplicationInfo[0]);
        for(int i = 0; i < apps.length; i++) {
        	strLog += apps[i].packageName + "Â¥n";
/*        	
        	try {
        		Context pContext = createPackageContext(apps[i].packageName, CONTEXT_RESTRICTED);
        		SharedPreferences sp = pContext.getSharedPreferences("CAReward_Media", Context.MODE_PRIVATE);
        		String sp_name = sp.getString("CARMUid", ""); 
        		
        		strLog += "SharedPreferences:" + String.valueOf(sp_name) + "Â¥n";
        		
        		Log.d(apps[i].packageName, String.valueOf(sp_name));
        		
        	} catch(NameNotFoundException e) {
        		Log.d("NameNotFoundException", "SharedPreferences");
        	} catch(Exception e) {
        		Log.d("Exception", "SharedPreferences");
        	}
*/
        }
        textLog.setText(strLog);
        
        btnContent.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
        		Intent intent = new Intent(TestCA.this, CARMIntent.class);
				intent.putExtra("m_id", edtMid.getText().toString());
				intent.putExtra("m_owner_id", edtOwnerId.getText().toString());
				intent.putExtra("user_id", edtUserId.getText().toString());
				intent.putExtra("platform_id", edtPlatformId.getText().toString());
				intent.putExtra("url", edtUrl.getText().toString());
				intent.putExtra("api_key", edtApiKey.getText().toString());
				intent.putExtra("app_key", edtAppKey.getText().toString());
				intent.putExtra("loading_msg", edtLoadingMsg.getText().toString());
				intent.putExtra("timeout", edtTimeout.getText().toString());
				
				/* æˆ»ã‚‹ãƒœã‚¿ãƒ³è¨­å®š */
				intent.putExtra("show_button", true);
				intent.putExtra("button_string", "ã‚‚ã�©ã‚‹ãƒœã‚¿ãƒ³");
				intent.putExtra("bk_color", Color.BLUE);
				intent.putExtra("button_gravity", Gravity.RIGHT);

        		startActivity(intent);
        	}
        });
    }
}
